﻿using HostAssistantBLL;
using HostAssistantCore;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HostAssistant
{
    public partial class editftp : U1Form
    {
        private string _id;
        public editftp(string id)
        {
            InitializeComponent();
            _id = id;
            lblhid.Visible = false;
            checkEdit1.Properties.RadioGroupIndex = 1;
            checkEdit2.Properties.RadioGroupIndex = 1;
            txtkj.ReadOnly = true;
           
        }
        #region 添加

        private void simpleButton1_Click(object sender, EventArgs e)
        {
          
            try
            {

             

               if(!checkEdit1.Checked  && !checkEdit2.Checked)
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("操作权限不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
               
                
                OpHelper.CallWithTimeout(Addmssql, 5000);

            }
            catch (Exception ex)
            {
                NLogger.Error(ex.ToString());
                
                    splashScreenManager1.CloseWaitForm();
                DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                if (lblhid.Text == "1")
                {
                    lblhid.Text = "0";
                    Form1 f1 = (Form1)this.Owner;
                    f1.op("FTP列表");
                    this.Dispose();
                }
              
            }

        }


         void Addmssql()
        {
            splashScreenManager1.ShowWaitForm();
            splashScreenManager1.SetWaitFormCaption("请等待");
            splashScreenManager1.SetWaitFormDescription("加载中...");
            
           string ftpname = this.txtuser.Text.Trim().ToString();
          
          
              
            
                
                    bool rw = false;
            if (checkEdit2.Checked)
                rw = true;

       

            IISManager iismanager = new IISManager();
                   iismanager.CreateFtppor(rw, ftpname);
                  

                string insert_sql = "update Ftpsite  set OperPower=@OperPower where   Name=@Name ";       //插入的SQL语句(带参数)
                Ftpsite Ftpsite = new Ftpsite();
            
                Ftpsite.Name = ftpname;
            
                Ftpsite.OperPower = (rw == true ? "读写" : "只读") ;
                Data<Ftpsite>.GetInstance().Update(insert_sql, Ftpsite);
            
                splashScreenManager1.CloseWaitForm();
                DevExpress.XtraEditors.XtraMessageBox.Show("成功修改权限", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
             

            

            lblhid.Text = "1";
        }

        #endregion

        #region 返回
        private void simpleButton2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion

      

      
        /// <summary>
        /// 请选择文件路径
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton3_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择文件路径";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                string foldPath = dialog.SelectedPath;
                txtkj.Text = foldPath;


            }
        }

        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton4_Click(object sender, EventArgs e)
        {
            //  txtpwd.Text = Guid.NewGuid().ToString().Substring(0,6);

            //  public static bool PasswordChange(string account, string password)
          
           
            updateftppwd f2 = new updateftppwd(this.txtuser.Text.Trim());
            f2.ShowDialog(this);


        }

        /// <summary>
        /// 用户名只能为字母、数字和下划线
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtuser_KeyPress(object sender, KeyPressEventArgs e)
        {
            this.txtuser.ImeMode = ImeMode.Off;

            if ((e.KeyChar >= '0' && e.KeyChar <= '9') || (e.KeyChar >= 'A' && e.KeyChar <= 'Z') ||
                  (e.KeyChar >= 'a' && e.KeyChar <= 'z') || (e.KeyChar == 8) || (e.KeyChar == '_'))
            {
                e.Handled = false;
            }

            else
            {
              //  MessageBox.Show("用户名只能为字母、数字和下划线！");
                DevExpress.XtraEditors.XtraMessageBox.Show("用户名只能为字母、数字和下划线！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);


                e.Handled = true;
            }
        }

        private void editftp_Load(object sender, EventArgs e)
        {
            var r=   ftpbll.GetFtpsite(_id);
            txtuser.Text = r?.Name ?? "";
            txtkj.Text = r?.Path ?? "";
            textEdit1.Text = r?.CreTime ?? "";
            if(r?.OperPower=="未知")
            {

            }
            else
           
                if (r?.OperPower == "只读")
                {
                checkEdit1.Checked = true;
                }
            else

                if (r?.OperPower == "读写")
            {
                checkEdit2.Checked = true;
            }



        }
    }
}
